package com.example.test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.test.adapter.WelcomeAdapter;
import com.example.test.fragment.OneFragment;
import com.example.test.fragment.TwoFragment;
import com.example.test.fragment.WelcomeFragment;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {

    private static final String TAG = "WelcomeActivity";

    private WelcomeFragment welcomeFragment;

    private ViewPager vp;
    private LinearLayout ll;

    //存放fragment的集合
    private List<Fragment> list;
    //存放小圆点的集合
    private List<ImageView> imageViewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        vp = (ViewPager) findViewById(R.id.vp);
        ll = (LinearLayout) findViewById(R.id.ll);

        //引导页
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        boolean welcome1 = welcome.getBoolean("welcome", false);
        if (welcome1){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        //初始化list
        list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        welcomeFragment = new WelcomeFragment();
        list.add(welcomeFragment);

        //将引导页添加到适配器中
        vp.setAdapter(new WelcomeAdapter(getSupportFragmentManager(),list));

        imageViewList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {

            ImageView imageView = new ImageView(this);

            if (i == 0){
                imageView.setImageResource(R.drawable.ic_launcher_background);
            }else {
                imageView.setImageResource(R.mipmap.ic_launcher_round);
            }

            imageViewList.add(imageView);

            ll.addView(imageView);
        }

        //引导页
        SharedPreferences welcome2 = getSharedPreferences("welcome", MODE_PRIVATE);
        SharedPreferences.Editor edit = welcome2.edit();
        SharedPreferences.Editor welcome3 = edit.putBoolean("welcome", true);
        welcome3.commit();

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position == list.size()-1){
                    welcomeFragment.handler.sendEmptyMessage(100);
                }

                for (int i = 0; i < imageViewList.size(); i++) {
                    if (i == position){
                        imageViewList.get(i).setImageResource(R.drawable.ic_launcher_background);
                    }else {
                        imageViewList.get(i).setImageResource(R.mipmap.ic_launcher_round);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
